package com.zmn.oms.services.impl.orderpaydiscount;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.oms.model.entity.discount.OrderPayDiscount;
import com.zmn.oms.persistence.interfaces.discount.OrderPayDiscountDao;
import com.zmn.oms.services.interfaces.orderpaydiscount.OrderPayDiscountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：订单支付优惠明细服务
 *
 * @author yangshuangping
 * @date 2020/10/20 17:05
 */
@Slf4j
@Service
public class OrderPayDiscountServiceImpl implements OrderPayDiscountService {
    @Resource
    private OrderPayDiscountDao orderPayDiscountDao;

    @NacosValue(value = "${pay.discount.ruleId:0}", autoRefreshed = true)
    private Integer payDiscountRuleId;

    @Override
    public List<OrderPayDiscount> listOrderPayDiscountByOrderId(Long orderId) {
        if (Objects.isNull(orderId)) {
            return Collections.emptyList();
        }
        return orderPayDiscountDao.listOrderPayDiscountByOrderId(orderId);
    }

    @Override
    public void insertList(List<OrderPayDiscount> discounts) {
        if (CollectionUtil.isNullOrEmpty(discounts)) {
            return ;
        }
        discounts.forEach(discount -> {
            discount.setRuleId(payDiscountRuleId);
            orderPayDiscountDao.insertSelective(discount);
        });
    }
}
